/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import ejb.Utente;
import ejb.UtenteFacadeLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Gemma
 */
@Stateless
public class GestUtente implements GestUtenteLocal {
    @EJB
        // era così:  UtenteFacade utenteFacade;
        private UtenteFacadeLocal utenteFacade;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
  
    
    @Override
        public void addUtente(String nome, String mail, String password, String gruppo){
            Utente u = new Utente();
            u.setNome(nome);
            u.setMail(mail);
            u.setPassword(password);
            u.setGruppo(gruppo);
            
            utenteFacade.create(u);
        }
        
         public List<Utente> findAll() {
        return utenteFacade.findAll();
    }



    @Override
        public boolean login_attempt(String mail, String password){
        List<Utente> utenti = utenteFacade.findAll();
        for (int i = 0; i < utenti.size(); i++) {
            if(utenti.get(i).getMail().equals(mail) && utenti.get(i).getPassword().equals(password))
                return true;
        }
    return false;
    }
    
     @PersistenceContext
    public EntityManager em;

    @Override
    public String getMail(String mail, String password) {
        try {
            return (String) em.createNamedQuery("trovaMailAuth").setParameter("mail", mail).setParameter("password", password).getSingleResult();
        } catch (NoResultException e) {
            return "getMail: nessun risultato";
        }

    }
    @Override
     public String getPassword(String mail, String password) {
        try {
            return (String) em.createNamedQuery("trovaPasswordAuth").setParameter("mail", mail).setParameter("password", password).getSingleResult();
        } catch (NoResultException e) {
            return "getPassword: nessun risultato";
        }
    }

    @Override
    public String getGruppo(String mail, String password) {
        try {
            return (String) em.createNamedQuery("trovaGruppoAuth").setParameter("mail", mail).setParameter("password", password).getSingleResult();
        } catch (NoResultException e) {
            return "getGruppo: nessun risultato";
        }
    }
    
        public String getNome(String mail, String password) {
        try {
            return (String) em.createNamedQuery("trovaNomeAuth").setParameter("mail", mail).setParameter("password", password).getSingleResult();
        } catch (NoResultException e) {
            return "getNome: nessun risultato";
        }
    }
    
    @Override
    public String getID(String mail, String password) {
        try {
            return (String) em.createNamedQuery("trovaIDAuth").setParameter("mail", mail).setParameter("password", password).getSingleResult();
        } catch (NoResultException e) {
            return "getID: nessun risultato";
        }
    }

    @Override
        public String getMailByID(String idUtente) {
        try {
            return (String) em.createNamedQuery("trovaMailByID").setParameter("idUtente", idUtente).getSingleResult();
        } catch (NoResultException e) {
            return "Errore: id non presente";
        } 
    }
    @Override
    public String getIDByMail(String mail) {
        try {
            return (String) em.createNamedQuery("trovaIDByMail").setParameter("mail", mail).getSingleResult();
        } catch (NoResultException e) {
            return "getIDByMail: nessun risultato";
        } 
    }
}
